package com.sky.controller.admin;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.OrderService;
import com.sky.service.SetmealService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 山顶洞人郑某
 * @className WorkspaceController.java
 * @date 2024 02 27
 */
@RequestMapping("/admin/workspace")
@RestController
@Slf4j
@Api(tags = "工作台")
public class WorkspaceController {

   @Autowired
   private OrderMapper orderMapper;
   @Autowired
   private SetmealService setmealService;
   @Autowired
   private DishService dishService;

   @Autowired
   private OrderService orderService;

   @Autowired
   private UserMapper userMapper;
   /**
    * 查询套餐总览
    * @return
    */
   @ApiOperation("查询套餐总览")
   @GetMapping("/overviewSetmeals")
   public Result<SetmealOverViewVO> overviewSetmeals(){
      SetmealOverViewVO setmeal =  setmealService.getoverviewOrders();
      log.info("停售套餐数量：：{},起售套餐数量::{}",setmeal.getDiscontinued(),setmeal.getSold());
      return Result.success(setmeal);
   }

   @GetMapping("/overviewDishes")
   @ApiOperation("查询菜品总览")
   public Result<DishOverViewVO> overviewDishes(){
           DishOverViewVO dishOverViewVO = dishService.getOverviewDishes();
           log.info("停售菜品数量::{},起售菜品数量::{}",dishOverViewVO.getDiscontinued(),dishOverViewVO.getSold());
           return Result.success(dishOverViewVO);
   }

   /**
    * 查询订单管理数据
    * @return
    */
   @GetMapping("/overviewOrders")
   @ApiOperation(value = "查询订单管理数据")
   public Result<OrderOverViewVO> overviewOrders(){
     OrderOverViewVO orderOverViewVO = orderService.getOverviewOrders();
     return Result.success(orderOverViewVO);
   }

   /**
    * 查询今日运营数据
    * @return
    */
   @GetMapping("/businessData")
   @ApiOperation("查询今日运营数据")
   public Result<BusinessDataVO> businessData(){
      BusinessDataVO businessDataVO = new BusinessDataVO();
      Map<String,Object> map = new HashMap<>();
      map.put("begin",LocalDateTime.of(LocalDate.now(),LocalTime.MIN));
      businessDataVO.setNewUsers(userMapper.countByMap(map));
      businessDataVO.setOrderCompletionRate(orderService.getOrderCompletionRate());
      businessDataVO.setTurnover(orderService.getTurnover());
      Integer userSum = userMapper.countByMap(new HashMap<>());
      if (userSum == 0){
         businessDataVO.setUnitPrice(0.0);
      }else {
         businessDataVO.setUnitPrice(businessDataVO.getTurnover() / userSum);
      }
      businessDataVO.setValidOrderCount(orderMapper.getOrderCountByStatus(Orders.COMPLETED));
      log.info("今日数据查询结果为{}",businessDataVO);
      return Result.success(businessDataVO);
   }
 }

